package com.hydl.power.service.impl;


import com.github.pagehelper.PageInfo;
import com.hydl.power.mapper.*;
import com.hydl.power.pojo.*;
import com.hydl.power.service.AppUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AppUserServiceImpl implements AppUserService {


    @Autowired
    private PowerRoomMapper powerRoomMapper;

    @Autowired
    private IncomingCabinetMapper incomingCabinetMapper;

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private LoadMapper loadMapper;

    @Autowired
    private TransformerMapper transformerMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private OrderDeliveryMapper orderDeliveryMapper;

    @Autowired
    private CityMapper cityMapper;

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private InspectionplanMapper inspectionplanMapper;

    @Autowired
    private WorkOrderMapper workOrderMapper;

    @Autowired
    private InspectionOrderMapper InspectionOrderMapper;

    @Autowired
    private TemperatureDataMapper temperatureDataMapper;

    @Autowired
    private TemperatureDeviceMapper temperatureDeviceMapper;

    @Autowired
    private DirectCurrentScreenMapper directCurrentScreenMapper;


  @Override
  public Map<String, String> electricQuantity(String token) throws ParseException{
        Map map=new HashMap();  //最后返回map
        SysUser sysUser=sysUserMapper.getUserByToken(token);
          if(sysUser==null){
              return map;
          }
        Project pro = projectMapper.queryProjectByProId(sysUser.getProjectId().toString());
        if(pro==null){
            return map;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int deviceCount=0;
        String dayPowerData ="";
        String monthPowerData ="";
        String sumPowerData ="";
        String dgaugingPointCount="";

        if(sysUser != null ){
            if(sysUser.getStatus() == 2 ){
                if(Integer.parseInt(pro.getPowerDaraFlag())==2){
                    List<Device> listDevice = deviceMapper.getDeviceTransformerStatus(pro.getProjectId());
                    if(listDevice.size()==0){
                        dayPowerData = temperatureDataMapper.queryePowereOneDay(pro.getTemperatureDataTableName(),pro.getProjectId());
                        if(dayPowerData==null){
                            dayPowerData="0";
                        }
                        monthPowerData  = temperatureDataMapper.queryePowereOneMonth(pro.getTemperatureDataTableName(),pro.getProjectId());
                        if(monthPowerData==null){
                            monthPowerData="0";
                        }
                        sumPowerData = temperatureDataMapper.queryePowereOneYear(pro.getTemperatureDataTableName(),pro.getProjectId());
                        if(sumPowerData==null){
                            sumPowerData="0";
                        }
                        dgaugingPointCount=temperatureDeviceMapper.getTemperatureDeviceCount(pro.getProjectId());
                    }else {
                        dayPowerData = incomingCabinetMapper.getDayPowerDataByProjectId(sysUser.getProjectId().toString());
                        if(dayPowerData==null){
                            dayPowerData="0";
                        }
                        monthPowerData  = incomingCabinetMapper.getMonthPowerDataByProjectId(sysUser.getProjectId().toString());
                        if(monthPowerData==null){
                            monthPowerData="0";
                        }
                        sumPowerData = incomingCabinetMapper.getSumPowerDataByProjectId(sysUser.getProjectId().toString());
                        if(sumPowerData==null){
                            sumPowerData="0";
                        }
                        dgaugingPointCount=deviceMapper.getDeviceNumberByProjectId(sysUser.getProjectId().toString());
                    }
                }else {
                    dayPowerData = incomingCabinetMapper.getDayPowerDataByProjectId(sysUser.getProjectId().toString());
                    if(dayPowerData==null){
                        dayPowerData="0";
                    }
                    monthPowerData  = incomingCabinetMapper.getMonthPowerDataByProjectId(sysUser.getProjectId().toString());
                    if(monthPowerData==null){
                        monthPowerData="0";
                    }
                    sumPowerData = incomingCabinetMapper.getSumPowerDataByProjectId(sysUser.getProjectId().toString());
                    if(sumPowerData==null){
                        sumPowerData="0";
                    }
                     dgaugingPointCount=deviceMapper.getDeviceNumberByProjectId(sysUser.getProjectId().toString());
                }

              //客户项目安全运行天数
              Project project = projectMapper.queryProjectByProId(sysUser.getProjectId().toString());
              if(project == null){
                return map;
              }
              long timeNumber = ((System.currentTimeMillis() - simpleDateFormat.parse(project.getFoundTime()).getTime()) / 1000 / (60 * 60 * 24));



              int reportAnEmergency = orderDeliveryMapper.queryReportAnEmergency(sysUser.getProjectId().toString());

              int transformer = transformerMapper.queryDeviceTransformerCount(sysUser.getProjectId().toString());

              String SumTransformerCapacity =transformerMapper.queryTransformerCapacity(sysUser.getProjectId().toString());
                if(SumTransformerCapacity==null){
                    SumTransformerCapacity="0";
                }

            /*  if(Integer.parseInt(dgaugingPointCount)-transformer>0){
                 deviceCount =Integer.parseInt(dgaugingPointCount)-transformer;
              }*/


              int powerRoom = powerRoomMapper.selectCountByProjectId(sysUser.getProjectId().toString());

              int directCurrentCount = directCurrentScreenMapper.queryDirectCurrentCount(sysUser.getProjectId().toString());

              PowerRoom room = powerRoomMapper.queryVoltageBoard(sysUser.getProjectId().toString());
              if(room == null){
                map.put("highVoltageBoard",0);
                map.put("lowPressureTank",0);
              }else {
                map.put("highVoltageBoard",room.getHighVoltageBoard());
                map.put("lowPressureTank",room.getLowPressureTank());
              }

              map.put("projectName",project.getProjectName());
              map.put("dayPowerData",dayPowerData);
              map.put("monthPowerData",monthPowerData);
              map.put("sumPowerData",sumPowerData);
              map.put("timeNumber",timeNumber);
              map.put("dgaugingPointCount",dgaugingPointCount);
              map.put("reportAnEmergency",reportAnEmergency);
              map.put("transformer",transformer);
              //map.put("deviceCount",deviceCount);
              map.put("powerRoom",powerRoom);
              map.put("directCurrentCount",directCurrentCount);
              map.put("SumTransformerCapacity",SumTransformerCapacity);
              Date day=new Date();
              SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
              map.put("time",df.format(day));
              return map;
            }
        }

      return map;
  }

    @Override
    public List queryMaxinloadRate(String powerRoomId) {
        //取该配电房最大max 最小min视在功率
        List<Map> list=loadMapper.queryMaxinloadRate(powerRoomId);
        List<Map> listData=new ArrayList<>();
        Map map=new HashMap();
      String max="";
        String min="";
      //获取所有变压器容量
      List<Transformer> transformer=transformerMapper.selectByCondition(null,null,null,null,null);
      DecimalFormat df = new DecimalFormat("0.00%");
      if(transformer==null){
        return listData;
      }
      if(list!=null){
        for (Map map1 : list) {
          for (Transformer transformer1 : transformer) {
            if(map1.get("deviceName").equals(transformer1.getDeviceName())){
              max=df.format(Double.parseDouble(map1.get("max").toString())*1.732/Double.parseDouble(transformer1.getTransformerCapacity()));
              min=df.format(Double.parseDouble(map1.get("min").toString())*1.732/Double.parseDouble(transformer1.getTransformerCapacity()));
            }
          }
          map.put("deviceName",map1.get("deviceName"));
          map.put("max",max);
          map.put("min",min);
          listData.add(map);
          map=new HashMap();
        }
      }
      return listData;
    }

      @Override
      public PageInfo<OrderDelivery> queryProjectOrder(Integer pageNum, Integer pageSize, String token) {
        SysUser sysUser=sysUserMapper.getUserByToken(token);
        if(sysUser != null){
          if(sysUser.getStatus() == 2){
               List<OrderDelivery> list= orderDeliveryMapper.queryProjectOrder(pageNum,pageSize,sysUser.getProjectId().toString());
            return new PageInfo<>(list);
          }
        }
        return null;
      }

      @Override
      public List queryCityProject(String token) {
        SysUser sysUser=sysUserMapper.getUserByToken(token);
        List list = new ArrayList();
        Map map = new HashMap();
        if(sysUser != null){
          if(sysUser.getStatus() == 2){
            List<City> city = cityMapper.queryCityByToken(sysUser.getProjectId().toString(),token);
            for (City city1 : city) {
              List<Project> project = projectMapper.queryProjectByToken(city1.getCityId(),token);
              city1.setChildren(project);
              for (Project project1 : project) {
                List<PowerRoom> powerRoom = powerRoomMapper.selectNavbarByNavId(project1.getProjectId());
                project1.setChildren(powerRoom);
                for (PowerRoom room : powerRoom) {
                  List<Device> device = deviceMapper.getDeviceByPowerRoomId(room.getRoomId());
                  room.setChildren(device);
                }
              }
            }
            return city;
          }
        }
        return null;
      }

  @Override
  public PageInfo<Inspectionplan> queryAppUserInspectionplan(Integer pageNum, Integer pageSize, String token, String creatTime) {
    SysUser sysUser = sysUserMapper.getUserByToken(token);
    List<Inspectionplan> list = new ArrayList<>();
    if(sysUser != null){
        if(sysUser.getStatus() == 2){
          list = inspectionplanMapper.queryAppUserInspectionplan(pageNum,pageSize,sysUser.getProjectId().toString(),creatTime);
        }
    }
    return new PageInfo<>(list);
  }
//客户端
  @Override
  public int queryProjectOrderNumberSum(String token) {
    SysUser sysUser = sysUserMapper.getUserByToken(token);
    int count = 0;
    if(sysUser != null){
      if(sysUser.getStatus() == 2){
           count = workOrderMapper.queryProjectOrderNumberSum(sysUser.getProjectId().toString());
      }
    }
    return count;
  }
  //运维端
  @Override
  public PageInfo<Inspectionplan> queryAppUserInspectionplans(Integer pageNum, Integer pageSize, String token, String creatTime) {
    SysUser sysUser = sysUserMapper.getUserByToken(token);
    List<Inspectionplan> list = new ArrayList<>();
    if(sysUser != null){
      if(sysUser.getStatus() != 2){
        list = inspectionplanMapper.queryAppUserInspectionplan(pageNum,pageSize,null,creatTime);
      }
    }
    return new PageInfo<>(list);
  }

  @Override
  public List<PowerRoom> getUserPowerRoom(String token) {
    SysUser user = sysUserMapper.getUserByToken(token);
    List<PowerRoom> list = new ArrayList<>();
    if(user!=null){
      if(user.getStatus()==2){
          list= powerRoomMapper.selectNavbarByNavId(user.getProjectId().toString());
      }
    }
    return list;
  }

    @Override
    public List<Transformer> queryDeviceTransformer(String token) {
        List<Transformer> list = new ArrayList<>();
        SysUser user = sysUserMapper.getUserByToken(token);
        if(user!=null){
            if(user.getStatus()==2) {
                list = transformerMapper.queryDeviceTransformer(user.getProjectId().toString());
            }
        }
        return list;
    }
}
